#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
import json
import sys
import urllib2

def getUsers(page=0,size=100):
    count = page*size
    conn = MySQLdb.connect(host="keepfitdb.mysql.rds.aliyuncs.com",user="for_ol",passwd="Abcd1234",port=3306)
    cur = conn.cursor()
    conn.select_db('coacht_uc')
    sql = "select id,real_name,tel,wx_no from t_user where real_name = '' or tel = '' or wx_no = '' or real_name is null or tel is null or wx_no is null limit %d , %d " % (count , size)
    cur.execute(sql)
    rows = cur.fetchall()
    cur.close()
    conn.close()
    return rows
def updateUser(id,real_name,tel,wx_no):
    conn = MySQLdb.connect(host="keepfitdb.mysql.rds.aliyuncs.com",user="for_ol",passwd="Abcd1234",port=3306)
    cur = conn.cursor()
    conn.select_db('coacht_uc')
    cur.execute('update t_user set real_name = %s , tel = %s, wx_no = %s where id = %s',(real_name,tel,wx_no,id))
    cur.close()
    conn.commit()
    conn.close()

def getUserWenjuan(uid):
    conn = MySQLdb.connect(host="keepfitdb.mysql.rds.aliyuncs.com",user="for_ol",passwd="Abcd1234",port=3306)
    cur = conn.cursor()
    conn.select_db('coacht')
    sql = "select name,tel,wx_no from t_user_wenjuan where uid = %s and status = 0" % uid
    cur.execute(sql)
    row = cur.fetchone()
    cur.close()
    conn.close()
    return row

def getUserByTel(tel):
    conn = MySQLdb.connect(host="keepfitdb.mysql.rds.aliyuncs.com",user="for_ol",passwd="Abcd1234",port=3306)
    cur = conn.cursor()
    conn.select_db('coacht_uc')
    sql = "select id from t_user where tel = '%s'" % tel
    cur.execute(sql)
    row = cur.fetchone()
    cur.close()
    conn.close()
    return row


if __name__ == "__main__":
    # user = getUserByTel('15275538980')
    # print user
    # if user != None:
    #     print 'not null'
    # sys.exit()
    size = 100
    page = 0
    while True:
        users = getUsers(page,size)
        uids =  ",".join(["%s" % user[0] for user in users])
        print uids
        # print users
        sys.exit()
        for user in users:
            try:
                id = user[0]
                real_name = user[1]
                tel = user[2]
                wx_no = user[3]
                wenjuan = getUserWenjuan(id)
                if not wenjuan :
                    continue

                wj_name = wenjuan[0]
                wj_tel = wenjuan[1]
                wj_wx = wenjuan[2]
                if wj_tel != "" and wj_tel != None:
                    oldUser = getUserByTel(wj_tel)
                    if oldUser != None:
                        continue

                if (not real_name) and wj_name :
                    real_name = wj_name
                if (not tel) and wj_tel :
                    tel = wj_tel
                if (not wx_no) and wj_wx :
                    wx_no = wj_wx

                print user
                print wenjuan
                print (id,real_name,tel,wx_no)
                print "\n"
                # updateUser(id,real_name,tel,wx_no)
            except Exception,e:
                print e
                # print str(id)+"数据异常"
        page += 1
        sys.exit()
        if len(users) < 100:
            break
